﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetworkIndexer.CoreExtensions.ServiceLocation;
using NetworkIndexer.Core.Repositories;
using NetworkIndexer.Data.DomainModels;
using System.Configuration;

namespace NetworkIndexer
{
    class Program
    {
        static void Main(string[] args)
        {

            var sharedComputers = new List<string>();
            IncludeIPs(sharedComputers);
            var searchExtensions = ConfigurationManager.AppSettings.Get("SearchExtensions");
            var completeRefresh = Convert.ToBoolean(ConfigurationManager.AppSettings.Get("CompleteRefresh"));
            var htmlPage = new Utilities.HtmlCreator();            
            htmlPage.Init();

            var shareDetails = ServiceLocator.GetInstance<IIndexingRepository>().Index(sharedComputers, searchExtensions, completeRefresh);

            foreach (ShareDetails share in shareDetails)
            {
                foreach (var file in share.files)
                {
                    htmlPage.AddLink(file, file);
                }
            }

            htmlPage.Finalise();
            htmlPage.CreateFile(ConfigurationManager.AppSettings.Get("FilePath"), string.Format("{0}.html", Guid.NewGuid()));
        }

        private static void IncludeIPs(List<string> sharedComputers)
        {
            var ipAddressWildCard = ConfigurationManager.AppSettings.Get("IPAddressWildcard");
            var ipAddressRange1 = ConfigurationManager.AppSettings.Get("IPAddressRange1");
            var ipAddressRange2 = ConfigurationManager.AppSettings.Get("IPAddressRange2");
            for (int i = Convert.ToInt32(ipAddressRange1.Split('-')[0]); i <= Convert.ToInt32(ipAddressRange1.Split('-')[1]); i++)
            {
                for (int j = Convert.ToInt32(ipAddressRange2.Split('-')[0]); j < Convert.ToInt32(ipAddressRange2.Split('-')[1]); j++)
                {
                    sharedComputers.Add(string.Format(ipAddressWildCard, i, j));
                }
            }
            
        }
    }
}
